package Action.Manager;

import Action.Sys.Setting;
import Bean.Log.HistoricalData;
import com.opensymphony.xwork2.ActionSupport;
import org.hibernate.Session;
import tools.HibernateTools;
import tools.TimeTools;

import java.sql.Timestamp;
import java.util.Arrays;

public class OrderStatistics extends ActionSupport {
    long sum;
    long[] data=new long[3];
    long[] data1=new long[3];
    long per=100;
    String per1;
    String err;
    Session session;
    int[] ordernums=new int[30];
    String[] order_date=new String[30];

    @Override
    public String execute() throws Exception {

        session= HibernateTools.getSession();
        sum=(long) session.createQuery("select count (*) from LoanBookLogRecord ").iterate().next();
        data[0]=(long)session.createQuery("select count (*) from LoanBookLogRecord where state=?").setParameter(0, Setting.LoanBook_state_reservation).iterate().next();
        data[1]=(long)session.createQuery("select count (*) from LoanBookLogRecord where state=?").setParameter(0, Setting.LoanBook_state_need_Return).iterate().next();
        data[2]=(long)session.createQuery("select count (*) from LoanBookLogRecord where state=?").setParameter(0, Setting.LoanBook_state_need_TimeOut).iterate().next();

        data1[0]=(long)session.createQuery("select count (*) from LoanBookLogRecord where state=?").setParameter(0, Setting.LoanBook_state_closed).iterate().next();
        data1[1]=(long)session.createQuery("select count (*) from LoanBookLogRecord where state=?").setParameter(0, Setting.LoanBook_state_cancel).iterate().next();
        data1[2]=(long) Arrays.stream(data).sum();
        per=100*data1[0]/sum;
        if (data1[2]==0){
            per1 = "无穷大";
        }
        else {
            per1=""+(double)data1[0]/data1[1];
        }
//        if (Arrays.stream(data).sum()==0){
//            err="当前没有未关闭的订单数据";
//        }
//        else err="";

        HistoricalData hd;
        for (int i = 0; i < ordernums.length; i++) {
            Timestamp timestamp = TimeTools.formatTime(ordernums.length - i - 1);
            hd = session.get(HistoricalData.class, timestamp);
            if (hd==null){
                hd = new HistoricalData();
                hd.setTimestamp(TimeTools.formatTime(ordernums.length-i-1));
                session.saveOrUpdate(hd);
            }
            ordernums[i]= hd.getOrderNum();
            order_date[i]=TimeTools.timeStamp2Date(timestamp, "dd");
        }

        HibernateTools.commit(session);



        return SUCCESS;
    }

    public long getSum() {
        return sum;
    }

    public void setSum(long sum) {
        this.sum = sum;
    }

    public long[] getData() {
        return data;
    }

    public void setData(long[] data) {
        this.data = data;
    }

    public long[] getData1() {
        return data1;
    }

    public void setData1(long[] data1) {
        this.data1 = data1;
    }

    public long getPer() {
        return per;
    }

    public void setPer(long per) {
        this.per = per;
    }

    public String getErr() {
        return err;
    }

    public void setErr(String err) {
        this.err = err;
    }

    public int[] getOrdernums() {
        return ordernums;
    }

    public void setOrdernums(int[] ordernums) {
        this.ordernums = ordernums;
    }

    public String[] getOrder_date() {
        return order_date;
    }

    public void setOrder_date(String[] order_date) {
        this.order_date = order_date;
    }

    public String getPer1() {
        return per1;
    }

    public void setPer1(String per1) {
        this.per1 = per1;
    }
}
